
<Serializable(), ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> Friend Class Vars
    Private m_description As String = ""
    Private m_firstNode As Var = Nothing

    Friend Sub New(ByVal Description_ As String)
        m_description = Description_
    End Sub
    Friend Sub addNode(ByRef Var_ As Var)
        If Not IsNothing(Var_) Then
            If IsNothing(m_firstNode) Then
                m_firstNode = Var_
            Else
                If m_firstNode.Name <> Var_.Name Then
                    m_firstNode.addNode(Var_)
                End If
            End If
        End If
    End Sub
    Friend Sub addNode(ByRef Var_() As Var)
        If Not IsNothing(Var_) Then
            If Var_.Length >= 1 Then
                For Each va As Var In Var_
                    If IsNothing(m_firstNode) Then m_firstNode = va Else m_firstNode.addNode(va)
                Next
            End If
        End If
    End Sub
    Friend Function length() As Integer
        Dim retInt As Integer = 0
        If Not IsNothing(m_firstNode) Then
            retInt = m_firstNode.lengthTotal
        End If
        Return retInt
    End Function
    Friend Function count() As Integer
        Dim intReturn As Integer = 0
        If Not IsNothing(m_firstNode) Then
            intReturn = m_firstNode.count
        End If
        Return intReturn
    End Function

    Public Property Description() As String
        Get
            Return m_description
        End Get
        Set(ByVal value As String)
            m_description = value
        End Set
    End Property
    Friend Function getFirst() As Var
        If IsNothing(m_firstNode) Then
            Return Nothing
        Else
            Return m_firstNode
        End If
    End Function
    Friend Sub Clear()
        m_firstNode = Nothing
    End Sub

    Default Friend ReadOnly Property Index(ByVal Ind As Integer) As Var
        Get
            Return Me.getByIndex(Ind)
        End Get
    End Property
    Default Friend ReadOnly Property Index(ByVal Key As String) As Var
        Get
            Return getByName(Key)
        End Get
    End Property
    Friend Function getByName(ByVal Name_ As String) As Var
        Dim f_cursor As Var
        Dim found As Boolean = False
        Name_ = LCase(Name_)
        If Not IsNothing(m_firstNode) Then
            f_cursor = m_firstNode
            If LCase(f_cursor.Name) = Name_ Then
                found = True
                GoTo endMod
            End If
            While Not found
                If Not IsNothing(f_cursor.getnext) Then
                    f_cursor = f_cursor.getnext
                    If LCase(f_cursor.Name) = Name_ Then
                        found = True
                        GoTo endMod
                    End If
                Else
                    found = False
                    GoTo endMod
                End If
            End While
        Else
            Return Nothing
        End If
endMod:
        If found Then Return f_cursor Else Return Nothing
    End Function
    Friend Function getByAddress(ByVal Address_ As Integer) As Var
        Dim f_cursor As Var
        Dim found As Boolean = False
        If Not IsNothing(m_firstNode) Then
            f_cursor = m_firstNode
            If f_cursor.Address = Address_ Then
                found = True
                GoTo endMod
            End If
            While Not found
                If Not IsNothing(f_cursor.getnext) Then
                    f_cursor = f_cursor.getnext
                    If f_cursor.Address = Address_ Then
                        found = True
                        GoTo endMod
                    End If
                Else
                    found = False
                    GoTo endMod
                End If
            End While
        Else
            Return Nothing
        End If
endMod:
        If found Then Return f_cursor Else Return Nothing
    End Function
    Friend Function getByIndex(ByVal Index_ As Integer) As Var
        Dim f_cursor As Var
        Dim found As Boolean = False
        If Not IsNothing(m_firstNode) Then
            f_cursor = m_firstNode
            If Index_ = 0 Then
                Return f_cursor
                found = True
            Else
                For count As Integer = 1 To Index_
                    If Not IsNothing(f_cursor.getnext) Then
                        f_cursor = f_cursor.getnext
                        found = True
                    Else
                        Return Nothing
                        Exit For
                    End If
                Next
            End If
        Else
            Return Nothing
        End If
        If found = False Then Return Nothing Else Return f_cursor
    End Function
    Friend Function getAsArray() As Var()
        Dim f_count As Integer
        Dim f_varArray() As Var = Nothing
        If Not IsNothing(m_firstNode) Then
            f_count = m_firstNode.count
            ReDim f_varArray(0 To f_count - 1)
            For i As Integer = 0 To f_count - 1
                f_varArray(i) = Me.getByIndex(i)
            Next
        End If
        Return f_varArray
    End Function
    Friend Function removeByAddress(ByVal Address_ As Integer) As Boolean
        Dim s_first As Var
        Dim s_temp As Var
        Dim found As Boolean = False

        If Not IsNothing(m_firstNode) Then
            If m_firstNode.Address = Address_ Then
                If IsNothing(m_firstNode.getnext) Then
                    m_firstNode = Nothing
                Else
                    s_temp = m_firstNode.getnext
                    m_firstNode = Nothing
                    Me.addNode(s_temp)
                End If
            Else
                ' First node not a match.. now check all other nodes
                s_first = m_firstNode
                Do
                    If s_first.getnext.Address = Address_ Then
                        'next node is a match..
                        If IsNothing(s_first.getnext.getnext) Then
                            'next node has no next.. so just null delete the link
                            s_first.setNext(Nothing)
                            found = True
                            GoTo endSub
                        Else
                            'the next node has a next.. so must swap the links
                            s_temp = s_first.getnext.getnext
                            s_first.setNext(s_temp)
                            found = True
                            GoTo endSub
                        End If
                    End If
                    s_first = s_first.getnext
                Loop Until found Or IsNothing(s_first.getnext)
            End If
        End If
endSub:
        s_first = Nothing
        s_temp = Nothing
        If found Then Return True Else Return False
    End Function
    Friend Function removeByIndex(ByVal Index_ As Integer) As Boolean
        Dim ret As Boolean = False
        Dim f_map As Var = Me.getByIndex(Index_)
        If Not IsNothing(f_map) Then
            Me.removeByName(f_map.Name)
            ret = True
        End If
        Return ret
    End Function
    Friend Function removeByName(ByVal Name_ As String) As Boolean
        Dim s_first As Var
        Dim s_temp As Var
        Dim found As Boolean = False


        If Not IsNothing(m_firstNode) Then
            If LCase(m_firstNode.Name) = LCase(Name_) Then
                If IsNothing(m_firstNode.getnext) Then
                    m_firstNode = Nothing
                Else
                    s_temp = m_firstNode.getnext
                    m_firstNode = Nothing
                    Me.addNode(s_temp)
                End If
            Else
                ' First node not a match.. now check all other nodes
                s_first = m_firstNode
                Do
                    If LCase(s_first.getnext.Name) = LCase(Name_) Then
                        'next node is a match..
                        If IsNothing(s_first.getnext.getnext) Then
                            'next node has no next.. so just null delete the link
                            s_first.setNext(Nothing)
                            found = True
                            GoTo endSub
                        Else
                            'the next node has a next.. so must swap the links
                            s_temp = s_first.getnext.getnext
                            s_first.setNext(s_temp)
                            found = True
                            GoTo endSub
                        End If
                    End If
                    s_first = s_first.getnext
                Loop Until found Or IsNothing(s_first.getnext)
            End If
        End If
endSub:
        s_first = Nothing
        s_temp = Nothing
        If found Then Return True Else Return False
    End Function
End Class
